<body>
    <h2>角色管理</h2>
    <div style="margin:10px 0;"></div>
    <table id="list"></table>
    <div id="dlg" class="easyui-dialog" style="width:400px;height:200px;padding:10px 20px"
            closed="true" buttons="#dlg-buttons">
        <div class="ftitle">角色</div>
        <form id="fm" method="post" novalidate>
            <div class="fitem">
                <label>名称:</label>
                <input name="name" class="easyui-validatebox" required="true">
            </div>
        </form>
    </div>

    <div id="ra-dia" class="easyui-dialog" style="width:600px;height:500px;padding:10px 20px" closed="true">
    	<div id="fileListLeft">
			<ul id="authRoleDiv"></ul>
		</div>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="roleListJs.save()">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
    </div>

    <script type="text/javascript">
(function(roleListJs, $, undefined ) {      
    var role = null;
	$('#list').datagrid({
		title:'角色信息',
		iconCls:'icon-save',
		height:400,
		fitColumns:true,
		singleSelect:true,
		url:null,
		frozenColumns:[[    
	                    {title:'ID',field:'id',width:80,sortable:true}  
	                ]],  
	    columns:[[
	             	{title:'名称',field:'name',width:80,sortable:true},
	             ]],
		pagination:false,
		toolbar:[{  
            id:'btnadd',  
            text:'添加',  
            iconCls:'icon-add',  
            handler:newRole
        },{  
            id:'btnupdate',  
            text:'修改',  
            iconCls:'icon-edit',  
            handler:editRole
        },'-',{  
            id:'btndelete',  
            text:'删除',   
            iconCls:'icon-remove',  
            handler:destroyRole 
        },{
        	id:'btnrefresh',
        	text:'刷新',
        	iconCls:'icon-reload',
        	handler:refresh
        },'-',{
            id:'btnauthorize',
            text:'权限管理',
            iconCls:'icon-reload',
            handler:authorize
        }], 
	    onLoadSuccess: function(){   
	        $('#employee').datagrid('loaded');
	    }  
	});
	
	roleListJs.init = function(){
		
		refresh();
	};
	
	roleListJs.loadRoleData = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	var payload = jQuery.parseJSON(result.payload.value);
        	$('#list').datagrid('loadData',payload);
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	};
	function loadRoleAuth(result){
		var loading = true;
    	$('#authRoleDiv').tree({
			    url: 'dispatch/manage/role/auth/query?role='+role,
			    animate:true,
			    lines: true,
			    checkbox:true,
			    onlyLeafCheck : true,
			    loadFilter: function(data){
			    	
			    	var res = data.result;
			        if(res == "SUCCESS")
			        {
			        	return jQuery.parseJSON(data.payload.value);
			        }else if(res == "COMPONENT_ERROR")
			        {
			        	$.messager.alert("Error",data.payload.value);
			        }else
			        {
			        	$.messager.alert("Error",data.result);
			        }
			        
			    	
			    },
			    onLoadSuccess : function(node, data){
			    	loading = false;
			    },
			    onLoadError : function(arguments){
			    	loading = false;
			    },
			    onBeforeLoad : function(node,param){
			    	
			    	if(node == null)return true;
			    	if(node.children == null)
			    	{
			    		loading = true;
			    		return true;
			    	}
			    	else
			    		{
			    			loading = false;
			    			return false;
			    		}
			    },
			    onCheck : function(node, checked){
			    	if(!loading)
			    	{
			    		workbenchJs.loadData("dispatch/manage/role/auth/update?role="+role+"&action="+node.id+"&group="+node.group+"&enable="+checked,roleListJs.simpleResultHanding,"更新角色权限");
			    	}
			    					    	
			    },
			});
	};
	roleListJs.simpleResultHanding = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	$.messager.show({
                title: 'success',
                msg: "success"
            });
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	};
	
    var url;
    function newRole(){
        $('#dlg').dialog('open').dialog('setTitle','添加角色');
        $('#fm').form('clear');
        url = 'dispatch/manage/role/add';
    };
    function authorize(){
        var row = $('#list').datagrid('getSelected');
        if (row){
        	 $('#ra-dia').dialog('open').dialog('setTitle','角色权限');
        	 role = row.id;
        	 
        	 loadRoleAuth();
        }else
        {
        	$.messager.alert("Error",'请先选择一条记录');
        }
    };
    function refresh(){
    	workbenchJs.loadData("dispatch/manage/role/all",roleListJs.loadRoleData,"查询角色信息");
    };
    function editRole(){
        var row = $('#list').datagrid('getSelected');
        if (row){
            $('#dlg').dialog('open').dialog('setTitle','编辑角色');
            $('#fm').form('clear');
            $('#fm').form('load',row);
            url = 'dispatch/manage/role/update?id='+row.id;
        }else
        {
        	$.messager.alert("Error",'请先选择一条记录');
        }
    };
    roleListJs.save = function(){
        $('#fm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
                var result = eval('('+result+')');
                if (result.result == "SUCCESS"){
                    $.messager.show({
                        title: 'SUCCESS',
                        msg: result.payload.value
                    });
                    $('#dlg').dialog('close');        // close the dialog
                    refresh();
                } else {
                	$.messager.alert("Error",result.result);
                }
            }
        });
    };
    function destroyRole(){
        var row = $('#list').datagrid('getSelected');
        if (row){
            $.messager.confirm('Confirm','删除角色同时会删除与角色相关的数据，确定？?',function(r){
                if (r){
                	workbenchJs.loadData("dispatch/manage/role/delete?id="+row.id,roleListJs.deleteRoleResult,"删除角色");
                }
            });
        }
    };

    roleListJs.deleteRoleResult = function(result){
        refresh();
    };
}( window.roleListJs = window.roleListJs || {}, jQuery ));

    $(function(){
    	roleListJs.init();
    });    	

    </script>
    <style type="text/css">
        #fm{
            margin:0;
            padding:10px 30px;
        }
        .ftitle{
            font-size:14px;
            font-weight:bold;
            padding:5px 0;
            margin-bottom:10px;
            border-bottom:1px solid #ccc;
        }
        .fitem{
            margin-bottom:5px;
        }
        .fitem label{
            display:inline-block;
            width:80px;
        }
    </style>